<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?><?php defined( '_SEXEC' ) or die( 'Restricted access' ); ?>
<?php
	if(!isset($_SESSION['username']) && !isset($_SESSION['usertype']))
		header("location: index.php");
?>
<?php	

if(isset($submit))
{
	switch(trim($submit))
	{
		case "Save":
				b($document_name, "Enter Document Name");
				b($document_price, "Enter Document Prices");
				i($document_price, "Enter Document Prices in Number");
				if(count($arr) == 0) {
					$sql="insert into documents (document_name,document_price) values(\"$document_name\",\"$document_price\")";
					mysql_query($sql);
					$msg = "Document added succesful";
					$document_name=$document_price="";
				}
				break;
		case "Update":
				if($step == 2) {
					b($document_name, "Enter Document Name");
					b($document_price, "Enter Document Prices");
					i($document_price, "Enter Document Prices in Number");
					if(count($arr) == 0) {
						$sql="update documents set document_name=\"$document_name\", document_price=\"$document_price\" where id = '$id'";
						mysql_query($sql);
						$msg = "Document updated succesful";
						$document_name=$document_price="";
					} else {
				  		$btval="     Update     ";
				 	}
				} 
				else if(isset($eid)) {
					$sql="select * from documents where id = '$eid'";
					$result = mysql_query($sql);
					$row = mysql_fetch_array($result);
					$id = $row['id'];
					$document_name = $row['document_name'];
					$document_price = $row['document_price'];
					$step=2;
					$btval="     Update     ";
				}
				break;
		case "Delete":
				$sql="delete from documents where id = '$eid'";
				mysql_query($sql);
				$msg = "Document delete succesful";
				break;
	}
}

if(!isset($btval))
	$btval="     Save     ";
	


?>

<div id="title">Document Management</div>

<form name="document-form" method="post" action="">
<table border="0" cellspacing="3" cellpadding="3" align="center">
	<tr>
		<td colspan=2>
			<?php
				se();
			?>
		</td>
	</tr>
	<tr>
		<td align=right>Document Name:</td>
		<td><input name="document_name" type="text" value="<?php echo $document_name?>"/></td>
	</tr>
	<tr>
		<td align=right>Document Price (Rs.):</td>
		<td><input name="document_price" type="text" value="<?php echo $document_price?>"/></td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<input type=hidden name=id value="<?php echo $id?>" />
			<input type=hidden name=step value="<?php echo $step?>" />
			<input type="submit" name="submit" value="<?php echo $btval?>" class='button'/>
		</td>
	</tr>	
</table>
</form>

<?php
echo "<hr />";
$table = "documents";
$cols = "*";
$where = "";
$orderby = "order by id";
$sort = "asc";
$pagelength = 50;
$reparr = array();
gettable("$table",$cols,"$where","$orderby","$sort",$pg,"<a href='?eid=\$id&submit=Update' style='text-decoration:none'>Update</a>&nbsp;|&nbsp;<a href=\"?eid=\$id&submit=Delete\" onclick=\"javascript:return confirm('Are you sure you want to Delete?')\" style='text-decoration:none'>Delete</a>",Array("id"),$reparr);
?>